package com.thinkfit.jy.activity;

import android.media.MediaPlayer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.thinkfit.jy.R;
import com.thinkfit.jy.bean.TestBean;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.AndroidMediaPlayerFactory;
import xyz.doikki.videoplayer.player.VideoView;

public class TestActivity  extends MainActivity{

    RecyclerView recyclerView;
    LinearLayout ll_sport_back;
    ArrayList<TestBean> list = new ArrayList<>();
    TestAdapter testAdapter;
    int line = 5;
    TextView tvTime,tvStart;
    int current = 1;
    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, line);
    Disposable mDisposableCountDown;
    protected int getLayoutId() {
        return R.layout.item_test;
    }

    @Override
    public void init() {
        ll_sport_back = findViewById(R.id.ll_sport_back);
        recyclerView = findViewById(R.id.recyclerView);
        tvTime = findViewById(R.id.tv_time);
        tvStart = findViewById(R.id.tv_start);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(getAdapter());
//        initList();
//        getAdapter().setNewData(list);
        ll_sport_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dsq();
                current = 1;
                initList();
                getAdapter().setNewData(list);
                getAdapter().notifyDataSetChanged();
            }
        });
    }

    public void dsq(){
        Observable.interval(1,  TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposableCountDown = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        tvTime.setText((aLong+1)+"秒");
                    }

                    @Override
                    public void onError(Throwable e) {
                        closeDisposableCountDown();
                    }

                    @Override
                    public void onComplete() {
                        closeDisposableCountDown();
                    }
                });
    }

    public TestAdapter getAdapter(){
        if(testAdapter == null){
            testAdapter = new TestAdapter(getContext());
        }
        testAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                TestBean o = (TestBean) adapter.getData().get(position);
                if(current >= line*line){
                    ToastUtils.getInstance().show(getContext(),"恭喜你，成功啦");
//                    line++;
//                    if(line > 10){
//                        line = 5;
//                    }
//                    gridLayoutManager.setSpanCount(line);
                    closeDisposableCountDown();

                }
                if(current == o.getItem()){
                    current ++;
                    o.setCheck(true);
                    adapter.notifyItemChanged(position);
                }else {
                    ToastUtils.getInstance().show(getContext(),"点的不对哦");
                }

            }
        });
        return testAdapter;
    }


    public List<TestBean> initList(){
        list.clear();
        for (int i=0;i<line*line;i++){
            list.add(new TestBean(i+1,false));
        }
        Collections.shuffle(list);
        return list;
    }
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        closeDisposableCountDown();
    }

    @Override
    public void resetInfo(SerialPortManager.CurrentInfo info) {

    }

    public void closeDisposableCountDown(){
        if (mDisposableCountDown != null) {
            mDisposableCountDown.dispose();
            mDisposableCountDown = null;
        }
    }
}
